package com.village.zpark.interceptors;
//拦截登录:1 如果没登录，提示登录 2.如果登录，直接放行

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginHandlerInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        Object loginUser=session.getAttribute("loginUser");
        if (loginUser==null){
            request.setAttribute("msg","对不起你没有登录，请重新登录");
            //转发，使用request对象,转发同一次请求，后期可以获取数据
            request.getRequestDispatcher("/login.html").forward(request,response);
            return false;
        }else {
            return true;
        }
    }

}
